<?php


namespace App\Models;


use App\Services\ZlbService;
use Illuminate\Database\Eloquent\Model;

class ZlbAuthentic extends Model
{
    protected $table = "order_authentic";
    protected $primaryKey = 'id';
    public $timestamps = false;

    /**
     * 订单用户新增数据
     * @param $order
     * @param $member
     * @param $authentic
     * @param $risk
     * @param $filePath
     */
    public static function add($order,$member,$authentic,$risk,$filePath){
        $data = [
            'order_no'=>$order['trade_no'],
            'phone'=>$member['mobile'],
            'cert_name'=>$authentic['cert_name'],
            'cert_no'=>$authentic['cert_no'],
            'user_type'=>1,
            'lessee_type'=>2,
            'login_type'=>2,
            'certify_id'=>$authentic['certify_id'],
            'yidun_grade'=>isset($risk['model_details_score']) ? intval($risk['model_details_score']) : 0,
            'alipay_uid'=>$member['user_id'],
            'idcard_hash'=>$filePath,
            'idcard_front'=>$authentic['idcard_front'],
            'idcard_back'=>$authentic['idcard_back'],
            'create_time'=>dateFormat(time()),
            'login_time'=>dateFormat(time()),
        ];
        ZlbAuthentic::insertGetId($data);
    }

    /**
     * 订单用户上链
     * @param $order
     * @param $request
     * @param $notary
     */
    public static function set($order,$request,$notary){
        $res = ZlbService::format($notary);
        if($res['status'] == ZlbService::STATUS_YES){
            //成功
            ZlbService::requestLog('上传订单用户信息成功',$request,$res);
        }else{
            //失败
            ZlbService::requestLog('上传订单用户信息失败',$request,$res);
        }
        $data = [
            'tx_hash'=>$res['tx_hash'],
            'status'=>$res['status'],
            'chain_time'=>dateFormat(time()),
            'chain_params'=>ZlbService::paramsResponse($request,$notary),
        ];
        ZlbAuthentic::where(['order_no'=>$order['trade_no']])->update($data);
    }
}
